package cn.nubia.admin.application.service;

import cn.nubia.admin.application.bean.SoftComment;
import cn.nubia.admin.application.bean.SoftEvaluating;
import cn.nubia.admin.application.bean.SoftEvaluatingVO;
import cn.nubia.admin.application.query.SoftEvaluatingQuery;
import cn.nubia.admin.common.springmvc.WebContext;
import cn.nubia.admin.common.utils.DateUtil;
import cn.nubia.admin.sysconfig.bean.Category;
import cn.nubia.admin.sysconfig.bean.SystemLog;
import cn.nubia.admin.sysconfig.service.CategoryService;
import cn.nubia.admin.common.mybatis.bean.Paged;
import cn.nubia.admin.common.mybatis.bean.Query;
import cn.nubia.admin.common.mybatis.service.BaseServiceImpl;
import cn.nubia.admin.common.utils.BeanUtils;
import cn.nubia.admin.sysconfig.service.SystemLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2015/7/22.
 * 最美应用.
 */
@Service
public class SoftEvaluatingService extends BaseServiceImpl<SoftEvaluating> {


    @Autowired
    private CategoryService categoryService;

    @Autowired
    SystemLogService systemLogService;
    /**
     * 获取列表.
     *
     * @param softEvaluatingQuery
     * @return
     */
    public Paged<SoftEvaluatingVO> findList(SoftEvaluatingQuery softEvaluatingQuery) {
        Query<SoftEvaluating> query = Query.build(SoftEvaluating.class);
        query.addEq("id", softEvaluatingQuery.getId());
        query.addLike("title", softEvaluatingQuery.getTitle());
        query.addNotEq(SoftEvaluating.COLUMN_STATE, SoftEvaluating.STATES_DEL);
        query.addOrder(SoftEvaluating.COLUMN_ADDTIME, Query.DBOrder.DESC);
        query.setPaged(softEvaluatingQuery.getPageNo(), softEvaluatingQuery.getPageSize());
        Paged<SoftEvaluating> page = findPagedByQuery(query);

        List<SoftEvaluatingVO> list = new ArrayList<SoftEvaluatingVO>();
        List<SoftEvaluating> softList = page.getListData();

        for (SoftEvaluating softEvaluating : softList) {
            SoftEvaluatingVO softEvaluatingVO = new SoftEvaluatingVO();
            //复制参数
            BeanUtils.copyProperties(softEvaluating, softEvaluatingVO);
            //设置状态
            Integer state = softEvaluating.getState();
            if (state.intValue() == SoftEvaluating.STATES_DEL) {
                softEvaluatingVO.setStateName("删除");
            } else if (state.intValue() == SoftEvaluating.STATES_HIDDEN) {
                softEvaluatingVO.setStateName("隐藏");
            } else if (state.intValue() == SoftEvaluating.STATES_SHOW) {
                softEvaluatingVO.setStateName("显示");
            }
            //设置分类名称
            Integer categoryId = softEvaluating.getCategoryId();
            if (null != categoryId) {
                Category category = categoryService.get(categoryId);
                if (null != category) {
                    softEvaluatingVO.setCategoryName(category.getCategoryName());
                }
            }
            list.add(softEvaluatingVO);
        }

        //设置属性.
        Paged<SoftEvaluatingVO> softEvaluatingVOPaged = new Paged<SoftEvaluatingVO>();
        softEvaluatingVOPaged.setPageNo(page.getPageNo());
        softEvaluatingVOPaged.setPageSize(page.getPageSize());
        softEvaluatingVOPaged.setTotalCount(page.getTotalCount());
        softEvaluatingVOPaged.setListData(list);
        return softEvaluatingVOPaged;


    }

    /**
     * 保存最美应用.
     *
     * @param softEvaluating
     */
    public void save(SoftEvaluating softEvaluating) {
        Integer currTime = DateUtil.getCurrentTime();
        softEvaluating.setPublishTime(currTime);
        softEvaluating.setLastModifyTime(currTime);
        if (softEvaluating.getId() != null) {
            SoftEvaluating domain = get(softEvaluating.getId());
            softEvaluating.setAddTime(domain.getAddTime());
            update(softEvaluating);
            systemLogService.log(SystemLog.ModuleTypeEnum.SOFTEVALUATINGLIST, SystemLog.OperateTypeEnum.EDIT, softEvaluating.getId());
        } else {
            softEvaluating.setAddTime(currTime);
            softEvaluating.setState(SoftEvaluating.STATES_SHOW);
            insert(softEvaluating);
            systemLogService.log(SystemLog.ModuleTypeEnum.SOFTEVALUATINGLIST, SystemLog.OperateTypeEnum.ADD, softEvaluating.getId());

        }
    }


    /**
     * 设置类型.
     *
     * @param id    id
     * @param state 状态（-1 删除 0 隐藏 1 显示）
     */
    public void setState(Integer id, Integer state) {
        if (null != id) {
            SoftEvaluating softEvaluating = get(id);
            softEvaluating.setState(state);
            Integer adminId = WebContext.getCachedUser().getId();//WebContext.getCachedUser().getId()
            String  adminName = WebContext.getCachedUser().getName();//WebContext.getCachedUser().getName()
            Integer curr = DateUtil.getCurrentTime();
            softEvaluating.setLastModifyTime(curr);
            update(softEvaluating);
            if(state == SoftEvaluating.STATES_SHOW){
                systemLogService.log(SystemLog.ModuleTypeEnum.SOFTCOMMENTLIST, SystemLog.OperateTypeEnum.SHOW, id);
            }else if(state == SoftEvaluating.STATES_HIDDEN){
                systemLogService.log(SystemLog.ModuleTypeEnum.SOFTCOMMENTLIST, SystemLog.OperateTypeEnum.HIDDEN, id);
            }else if(state == SoftEvaluating.STATES_DEL){
                systemLogService.log(SystemLog.ModuleTypeEnum.SOFTCOMMENTLIST, SystemLog.OperateTypeEnum.DEL, id);
            }
        }
    }
}